<template>
	<view>
		<cu-custom bgColor="bgmain" :isBack="true">
			<view slot="content">个人资料</view>
		</cu-custom>

		<view class="margin-sm box padding">
			<view class="flex align-center justify-between margin-bottom border-bottom">
				<view class="text-df margin-right">头像</view>
				<view style="flex-grow: 1;" class="text-right">
					<image :src="avatar" @click="choose"></image>
				</view>
				<text class="cuIcon-right text-gray"></text>
			</view>
			<view class="flex align-center justify-between margin-bottom border-bottom">
				<view style="flex-shrink: 0" class="text-df margin-right">昵称</view>
				<input maxlength="16" placeholder="请输入昵称" v-model="nickname" />
				<text class="cuIcon-right text-gray"></text>
			</view>
			<view class="flex align-center justify-between " v-if="sys.is_real == 1">
				<view class="text-df margin-right" style="flex-shrink: 0;">认证状态</view>
				<input maxlength="16" value="已认证" disabled="" v-if="student == 1"/>
				<input maxlength="16" value="未通过" disabled="" @click="tiao()" v-if="student == 2"/>
				<input maxlength="16" value="未认证" disabled="" @click="tiao()" v-if="student == -1"/>
				<input maxlength="16" value="申请中" disabled="" v-if="student == 0"/>
				<text class="cuIcon-right text-gray"></text>
			</view>
		</view>
		
		<view class="btns" @click="submit">提交修改</view>

	</view>
</template>

<script>
	var app = getApp()
	import siteInfo from '@/siteinfo.js';
	export default {
		data() {
			return {
				avatar: '',
				nickname: '',
				student:-1,
				sys:''
			}
		},
		onLoad() {
			this.getUser()  
			this.setting()
			// this.getstudent()
		},
		onShow: function () {
			this.getstudent();
		},
		methods: {
			
			setting(){
				const self = this;
				app.globalData.util.request({
				    url: 'Setting/view',
				
				    success(res) {
				        self.setData({
							// sysparment: res.data.data.list[0].is_anonymous_switch
				            sys: res.data.data.sys
				        });
				    }
				},true);
			},
			tiao(){
				uni.navigateTo({
					url:'/gc_school/pages/user/student'
				})
			},
			getstudent(){
						 const self = this;
						 app.globalData.util.request({
						     url: 'Dmhstudent/views',
						 
						     success(res) {
						         self.setData({
						 			// sysparment: res.data.data.list[0].is_anonymous_switch
						             student: res.data.data.state
						         });
						     }
						 },true);
			},
			getUser() {
				const self = this;
				app.globalData.util.request({
					url: 'WechatUser/view',
					data: {
						s_id: uni.getStorageSync('schoolId')
					},
					success(res) {
						self.nickname = res.data.data.nickname
						self.avatar = res.data.data.avatar
					}
				}, true);
			},
			choose() {
				var self = this
				uni.chooseImage({
					count: 1,
					sizeType: ['original', 'compressed'],
					sourceType: ['album'],
					success: (res) => {
						console.log('path', res);
						uni.showLoading({
						    title: '上传中...',
						    mask: true
						});
						uni.uploadFile({
							url: siteInfo.url+'Base/upload',
							filePath: res.tempFilePaths[0],
							formData: {
								'wxapp_id':siteInfo.uniacid
							},
							header: {
								'content-type': 'application/x-www-form-urlencoded'
							},
							name: 'file',
							success: (result) => {
								var data = JSON.parse(result.data.replace('Array', ''))
								if(data.status==200)
								{
									self.avatar = data.data;
									uni.hideLoading();
								}else{
									uni.showToast({
										title:data.msg,
										icon:'none'
									})
								}
							}
						});
					}
				});
			},
			submit()
			{
				var nickname = this.nickname.replace(/[, ]/g,'')
				if(nickname=='')
				{
					uni.showToast({
						title:'昵称不能为空',
						icon:'none'
					})
					return;
				}
				if(this.avatar=='')
				{
					uni.showToast({
						title:'请上传头像',
						icon:'none'
					})
					return;
				}
				app.globalData.util.request({
					url: 'WechatUser/edit',
					data: {
						avatar:this.avatar,
						nickname:this.nickname
					},
					success(res) {
						if(res.data.status==200)
						{
							uni.showToast({
								title:res.data.msg
							})
							setTimeout(function(){
								uni.navigateBack({
									delta:1
								})
							},1000)
						}
						console.log(res)
					}
				}, true);
			}
		}
	}
</script>

<style lang="scss">
	.box {
		background-color: white;
		border-radius: 10rpx;

		image {
			width: 90rpx;
			height: 90rpx;
			border-radius: 50%;
			margin-right: 10rpx;
		}

		input {
			flex-grow: 1;
			// border: 1rpx solid black;
			text-align: right;
			margin-right: 10rpx;
		}

		.border-bottom {
			padding-bottom: 30rpx;
			border-bottom: 1rpx solid #F2F2F2;
		}
	}
	.btns {
	    margin: 150rpx 5% 60rpx;
	    width: 90%;
	    height: 90rpx;
	    line-height: 90rpx;
	    background: linear-gradient(270deg, #FF716C 0%, #FF484A 100%);
	    text-align: center;
	    border-radius: 60rpx;
	    font-size: 32rpx;
	    font-weight: bold;
		color: white;
	}
</style>
